$(function (){
   loadHomeworkTable(0);
});

function getSelectFilename(){
   let file = $("#file").val();
   $("#uploadFilename").val(file);
}

function uploadFile() {
   let files = $("#file").prop("files");
   let data = new FormData;
   data.append("file", files[0]);
   data.append("hid", $("#hid").val());
   data.append("sid", $("#sid").val());
   $.ajax({
      type: "POST",
      url: "webapi/homework/upload",
      data: data,
      processData: false, //无须序列化
      contentType: false, //无须处理
      cache: false //无须缓存
   }).done(function (data){
      if (data === 1){
         alert("提交成功");
         history.back();
      } else if (data === -1){
         alert("提交失败");
      }
   });
}

function loadHomeworkTable(page) {
   let tid = $("#tid").val();
   let sid = $("#sid").val();
   $.ajax({
      type: "GET",
      url: "webapi/homework/getByPage",
      data: {
         page: page,
         tid: tid
      }
   }).done(function (pageUtil){
      //console.log(pageUtil);
      let current = pageUtil.current;
      let list = pageUtil.list;
      let total = pageUtil.total;
      let totalPage = pageUtil.totalPage;

      let table = $("#homeworkTable");
      let pagination = $("#pagination");

      table.empty();
      pagination.empty();

      for (const key in list) {
         let $homework = list[key];
         let id = $("<td>" + $homework.id + "</td>");
         let title = $("<td>" + fixedStringLength($homework.title) + "</td>");
         let startDate = $("<td>" + formatDate($homework.startDate) + "</td>");
         let endDate = $("<td>" + formatDate($homework.endDate) + "</td>");
         let status = $("<td></td>");
         $.ajax({
            type: "GET",
            url: "webapi/homework/getHomeworkStatus",
            data: {
               hid: $homework.id,
               sid: sid
            }
         }).done(function (data) {
            console.log(data);
            if (data === 1){
               status.text("已提交");
            } else {
               status.text("未提交");
            }
         })
         let button = $("<td><a href='/toHomeworkDetail?hid=" + $homework.id + "' class='button primary icon small solid fa-search'>查看</a></td>")

         let homeworkInfo = $("<tr></tr>").append(id).append(title).append(startDate).append(endDate).append(status).append(button);
         table.append(homeworkInfo);
      }

      //页码处理
      pagination.append("<li><span class='button small' onclick='loadHomeworkTable(0)'>首页</span></li>");
      if (current > 0){
         pagination.append("<li><span class='button small' onclick='loadHomeworkTable(" + (current - 1) + ")'>上一页</span></li>");
      }
      for (let i = 0; i < totalPage; i++) {
         let pageNum = $("<li><span class='page' onclick='loadHomeworkTable(" + i + ")'>" + (i + 1) + "</span></li>");
         if (i === current){
            pageNum.children("span").addClass("active");
         }
         pagination.append(pageNum);
      }
      if (current < totalPage - 1){
         pagination.append("<li><span class='button small' onclick='loadHomeworkTable(" + (current + 1) + ")'>下一页</span></li>");
      }
      pagination.append("<li><span class='button small' onclick='loadHomeworkTable(" + (totalPage - 1) + ")'>尾页</span></li>");
   });
}